<?php

namespace App\Console\Commands;

use App\Http\Controllers\Resource\ZwzxController;
use App\Models\ResourceBusinessName;
use App\Models\ResourceZwzx;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class ZwzxMysqlData extends Command
{

    // 使用 命令行执行  php artisan command:ZwzxMysqlData

    protected $data_sync_hour = 0; //0 表示不限制时间，全同步，其余表示 更改时间  单位小时
    protected $limit = 100; //每页限制条数
    protected $start_page = 1; //开始页，默认为 1
    protected $resource_business_id = 1; //资源商id

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:ZwzxMysqlData';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '同步中文在线数据到数据库';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $resourceBusinessNameModel = new  ResourceBusinessName();
        $resource_business_name_info = $resourceBusinessNameModel->where('id', $this->resource_business_id)->where('is_del', 1)->first();
        if (empty($resource_business_name_info)) {
            Log::channel('zwzxcmdlog')->debug('资源商不存在或已被删除');
            die;
        }

        try {
            $start_page = $this->start_page ?? 1;
            $obj = new ZwzxController();
            $resourceModel = new ResourceZwzx();
            //  $res = $obj->bookSearch($modify, $status = 1, $keywords = null, $page = 1, $limit = 10);
            if ($this->data_sync_hour) {
                $modify = strtotime('-' . $this->data_sync_hour . ' hour', time()) . '000'; //多获取一个小时的数据
            } else {
                $modify = null;
            }
            $res = $obj->bookSearch($modify, 1, null, 1, 1); //计算总条数
            dump($res);
            die;
            $total = $res['totalCount'];
            $total_page = ceil($total / $this->limit);

            for ($i = $start_page; $i <= $total_page; $i++) {
                sleep(1); //等待1秒
                Log::channel('zwzxcmdlog')->debug('正在同步第 ' . $i . ' 页 范围为 ' . ((($i - 1) * $this->limit) + 1) . ' 至 ' . (($i) * $this->limit) . ' 的数据');
                $data = $obj->bookSearch($modify, 1, null, $i, $this->limit);
                if ($data) {
                    $result_id = '';
                    foreach ($data['list'] as $key => $val) {
                        $result = $resourceModel->addData($val);
                        $result_id .= $result . '、';
                    }
                    trim($result_id, '、');
                    Log::channel('zwzxcmdlog')->debug('同步完成: ' . $result_id);
                } else {
                    Log::channel('zwzxcmdlog')->debug('数据处理完毕: 页数: ' . $i . ' 每页条数: ' . $this->limit);
                    return false;
                }
            }

            Log::channel('zwzxcmdlog')->debug('数据全部同步完成');
            // echo '数据全部同步完成';
            //更新同步时间
            $resourceBusinessNameModel->where('id', $this->resource_business_id)->update(['last_synch_time' => date('Y-m-d H:i:s')]);
        } catch (\Exception $e) {
            Log::channel('zwzxcmdlog')->debug('数据处理出现异常:' . $e->getMessage() . ';File:' . $e->getFile() . ';Line:' . $e->getLine() . ';');
        }
    }
}
